#include "map.h"
#include "cube.h"

#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
using namespace std;


Map::Map(QWidget *parent, int newX, int newY, QString filepath) :
    QWidget(parent)
{
    counter = 0;
    startSpeed = 4;
    speed = startSpeed;
    x = newX;
    y = newY;
    up = down = left = right = false;

    shipWidth = 6;
    shipOffset = 5;

    background = new QImage(filepath + "Images/background02.png");
}

void Map::addCube(double newX, double newY, double newZ, int newSize, int  newColor)
{
    cout << "Adding a cube" << endl;
    Cube *newCube = new Cube(this, newX, newY, newZ, newSize, newColor);
}

void Map::camUpdate()
{
    if(up == true)
        if(y > 2)
            y -= 2;
    if(down == true)
        if(y < 45)
            y += 2;
    if(left == true)
        if(x > 1.5)
            x -= 2;
    if(right == true)
        if(x < 48.5)
            x += 2;
}

void Map::getMap()
{
    addCube(5, 5, 200, 10, 0);
    addCube(15, 15, 225, 10, 0);
    addCube(25, 25, 250, 10, 0);
    addCube(35, 35, 275, 10, 0);
    addCube(45, 45, 300, 10, 0);
    addCube(45, 5, 400, 10, 0);
    addCube(35, 15, 425, 10, 0);
    addCube(25, 25, 450, 10, 0);
    addCube(15, 35, 475, 10, 0);
    addCube(5, 45, 500, 10, 0);
    addCube(25, 25, 700, 10, 0);
    addCube(45, 45, 700, 10, 0);
    addCube(5, 15, 900, 10, 0);
    addCube(25, 15, 900, 10, 0);
    addCube(45, 15, 900, 10, 0);
    addCube(5, 35, 900, 10, 0);
    addCube(25, 45, 900, 10, 0);
    addCube(45, 45, 900, 10, 0);
    addCube(15, 15, 1200, 20, 0);
    addCube(35, 35, 1200, 20, 0);
    addCube (15, 35, 1300, 10, 0);
    addCube(35, 15, 1300, 10, 0);
    addCube(5, 15, 1500, 10, 0);
    addCube(25, 15, 1500, 10, 0);
    addCube(45, 15, 1500, 10, 0);
    addCube(5, 35, 1500, 10, 0);
    addCube(25, 45, 1500, 10, 0);
    addCube(45, 45, 1500, 10, 0);
    addCube(45, 45, 1800, 30, 0);
    addCube(5, 5, 2000, 25, 0);
    addCube(45, 5, 2000, 25, 0);
    // addCube(25, 25, 2000, 10, 0);
    addCube(5, 45, 2000, 25, 0);
    addCube(45, 45, 2000, 25, 0);
    addCube(25, 25, 2040, 40, 0);
    addCube(5, 5, 2200, 10, 0);
    addCube(15, 15, 2225, 10, 0);
    addCube(25, 25, 2250, 10, 0);
    addCube(35, 35, 2275, 10, 0);
    addCube(45, 45, 2300, 10, 0);
    addCube(45, 5, 2400, 10, 0);
    addCube(35, 15, 2425, 10, 0);
    addCube(25, 25, 2450, 10, 0);
    addCube(15, 35, 2475, 10, 0);
    addCube(5, 45, 2500, 10, 0);
    addCube(25, 25, 2700, 10, 0);
    addCube(45, 45, 2700, 10, 0);
    addCube(15, 15, 3200, 20, 0);
    addCube(35, 35, 3200, 20, 0);
    addCube (15, 35, 3300, 10, 0);
    addCube(35, 15, 3300, 10, 0);
    addCube(5, 15, 3500, 10, 0);
    addCube(25, 15, 3500, 10, 0);
    addCube(45, 15, 3500, 10, 0);
    addCube(5, 35, 3500, 10, 0);
    addCube(25, 45, 3500, 10, 0);
    addCube(45, 45, 3500, 10, 0);
    addCube(45, 45, 3800, 30, 0);
    addCube(5, 5, 4000, 25, 0);
    addCube(45, 5, 4000, 25, 0);
    addCube(5, 45, 4000, 25, 0);
    addCube(45, 45, 4000, 25, 0);
    addCube(25, 25, 4040, 40, 0);
    addCube(15, 35, 4200, 30, 0);
}

void Map::loadCubes()
{
    //Get the map
    getMap();


    this->speed = this->startSpeed;
    //Reverse engineer the position of the cubes
    for(int i = 0; i < this->counter; i++)
    {
        int x = 1;
        int y = 2;
        if(i % (y*30) == 0)
            this->speed += x;

        //Get each cube in the gameMap...
        QList<Cube *> allCubes = this->findChildren <Cube *>();

        for (int i = allCubes.size() - 1; i >= 0; --i)
        {
            //Z-update (Move the cube forward)
            allCubes[i]->zUpdate();

            if(allCubes[i]->z < 0)
                allCubes[i]->deleteLater();
        }
    }
}

void Map::saveMap(){
    QString filename = QFileDialog::getSaveFileName(this, "Save Form", "", "");
    QFile file(filename);

    if (!file.open(QIODevice::WriteOnly))
    {
        QMessageBox::information(this, tr("Unable to open file"), file.errorString());
        return;
    }

    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_4_6); // Change this based on current Qt version

    // output counter
    out << counter;
    //output x
    out << this->x;
    // output y
    out << this->y;
    //close file
    file.close();
}
void Map::loadMap(){

    QString filename = QFileDialog::getOpenFileName(this, "Load Form", "", "");
    QFile file(filename);

    if (!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(this, tr("Unable to open file"), file.errorString());
        return;
    }
    else
    {
        //Delete all the existing cubes
        //Get each cube in the gameMap...
        QList<Cube *> allCubes = this->findChildren <Cube *>();

        for (int i = allCubes.size() - 1; i >= 0; --i)
        {
            allCubes[i]->deleteLater();
        }

        QDataStream in(&file);
        in.setVersion(QDataStream::Qt_4_6);

        /////////////////////
        //Declare variables//
        /////////////////////
        int newcounter, newx, newy;
        /////////////////////
        //read in the counter//
        /////////////////////
        in >> newcounter;
        //////////////////////
        //Read in properties//
        //////////////////////
        in>>newx;
        in>>newy;
        ///////////////////////////////
        //Assign properties to object//
        ///////////////////////////////
        this->x = newx;
        this->y = newy;
        this->counter = newcounter;

        //Create the cubes to match these variables.
        this->loadCubes();
    }

    }
